package br.com.virtual.base.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.inject.Named;

@Named("cnpjConverter")
public class CNPJConverter implements Converter {

	@Override
	public Object getAsObject(FacesContext arg0, UIComponent arg1, String valor) {

		String cnpj = valor;
		if (valor != null && !valor.equals("")) {
			cnpj = valor.replaceAll("\\.", "").replaceAll("\\-", "")
					.replaceAll("\\/", "");
		}

		return cnpj;
	}

	@Override
	public String getAsString(FacesContext arg0, UIComponent arg1, Object valor) {

		String cnpj = valor.toString();

		if (cnpj != null && cnpj.length() == 11) {

			cnpj = cnpj.substring(0, 2) + "." + cnpj.substring(2, 5) + "."
					+ cnpj.substring(5, 8) + "/" + cnpj.substring(8, 12) + "-"
					+ cnpj.substring(12, 14);
		}
		return cnpj;
	}

}
